//
//  UserLoginViewController.m
//  UI-豆瓣项目APP
//
//  Created by dllo on 15/10/28.
//  Copyright © 2015年 孙鹏. All rights reserved.
//

#import "UserLoginViewController.h"
#import "UserLoginView.h"
#import "DateBaseSingleton.h"
#import "UserList.h"
@interface UserLoginViewController ()

@end

@implementation UserLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"注册";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(loginAction:)];
    self.view.backgroundColor = [UIColor whiteColor];
    self.userV = [[UserLoginView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    [self.view addSubview:self.userV];
#pragma mark -- 创建用户表
    [[DateBaseSingleton shareDateBase]creatUserTable];
    
    
    // Do any additional setup after loading the view.
}

- (void)loginAction:(UIBarButtonItem *)loginbtn
{
    
    
    
    NSMutableArray *array = [NSMutableArray array];
    array = [[DateBaseSingleton shareDateBase]selectUserName];
    
   
    
 
    
    
    if (self.userV.userName1.text.length == 0) {
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名不能为空" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertC animated:YES completion:^{
            
            
        }];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        [alertC addAction:action];
        
        
    }
    
    
    else  if (![self.userV.code1.text isEqualToString:self.userV.repeatCode1.text]&& self.userV.code1.text.length != 0) {
        
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"两次密码输入不一致" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertC animated:YES completion:^{
            
            
        }];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        [alertC addAction:action];
        
        
          NSLog(@"%@",self.userV.code1.text);
          NSLog(@"%@",self.userV.repeatCode1.text);
    }else if ([array containsObject:self.userV.userName1.text])
    {
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"该用户名已经被注册" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertC animated:YES completion:^{
            
            
        }];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        [alertC addAction:action];
        
        
        
        
    }else
    {
        
        UserList *user = [[UserList alloc]init];
        
        NSString *name = self.userV.userName1.text;
        user.userName = name;
        NSString *code = self.userV.code1.text;
        user.code = code;
        
        [[DateBaseSingleton shareDateBase]insertUser:user];
        
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertC animated:YES completion:^{
            
            
        }];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        [alertC addAction:action];
        
        
        
        
        
    }
    
    
    
 
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    

    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
